package com.group4.datafactorybackend.controller;

import com.group4.datafactorybackend.common.R;
import com.group4.datafactorybackend.entity.DTO.CreateOrUpdateDataSourceMessageDTO;
import com.group4.datafactorybackend.entity.DTO.BatchReleaseOrOutageDTO;
import com.group4.datafactorybackend.entity.DTO.ConnectTestDTO;
import com.group4.datafactorybackend.entity.DTO.SelectDataSourceMessageDTO;
import com.group4.datafactorybackend.service.DataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * @author aKUN
 * @date 2022/7/28
 * @description 数据源管理控制层
 */
@Api(tags = "数据库管理")
@RestController
@RequestMapping("dataSource")
public class DataSourceController {

    private final DataSourceService dataSourceService;

    @Autowired
    public DataSourceController(DataSourceService dataSourceService){
        this.dataSourceService = dataSourceService;
    }

    /**
     * @author aKUN
     * @param selectDataSourceMessageDTO 用于查询和分页展示
     * @return R
     * @description 数据库管理分页
     **/
    @ApiOperation("查询数据源列表")
    @PostMapping("/post/selectDateSourceByPage")
    public R<?> selectDateSourceByPage(@RequestBody @Validated SelectDataSourceMessageDTO selectDataSourceMessageDTO){
        return dataSourceService.selectDateSourceByPage(selectDataSourceMessageDTO);
    }

    /**
     * @author OKer
     * @return R
     * @description 查询所有数据库类型
     **/
    @ApiOperation("查询数据库类型列表")
    @GetMapping("/get/findAllDatabaseType")
    public R<?> findAllDatabaseType(){
        return dataSourceService.findAllDatabaseType();
    }

    /**
     * @author OKer
     * @param createOrUpdateDataSourceMessageDTO 新增、编辑数据源信息接收参数实体类
     * @return R
     * @description 新增数据源连接信息
     **/
    @ApiOperation("新增数据源")
    @PostMapping("/post/addDataSource")
    public R<?> addDataSource(@RequestBody @Validated CreateOrUpdateDataSourceMessageDTO createOrUpdateDataSourceMessageDTO){
        return dataSourceService.addDataSource(createOrUpdateDataSourceMessageDTO);
    }

    /**
     * @author 牟屿洋
     * @param createOrUpdateDataSourceMessageDTO 新增、编辑数据源信息接收参数实体类
     * @return R
     * @description 编辑数据源信息
     */
    @ApiOperation("编辑数据源信息")
    @PutMapping("/update/updateDataSource")
    public R<?> updateDataSource(@RequestBody @Validated CreateOrUpdateDataSourceMessageDTO createOrUpdateDataSourceMessageDTO){
        return dataSourceService.updateDataSource(createOrUpdateDataSourceMessageDTO);
    }

    /**
     * @author 牟屿洋
     * @param batchReleaseOrOutageDTO （批量）发布/停用接收参数实体类
     * @return R
     * @description 发布/停用数据源信息
     */
    @ApiOperation("发布/停用数据源信息")
    @PutMapping("/update/releaseOrOutage")
    public R<?> releaseOrOutage(@RequestBody BatchReleaseOrOutageDTO batchReleaseOrOutageDTO){
        return dataSourceService.releaseOrOutage(batchReleaseOrOutageDTO);
    }

    /**
     * @author 卑微小峰
     * @param connectTestDTO 数据源连通测试接收参数实体类
     * @return R
     * @description 连接测试数据源
     */
    @ApiOperation("连接测试数据源")
    @PostMapping("/post/connectTest")
    public R<?> connectTest(@RequestBody @Validated ConnectTestDTO connectTestDTO) {
        return dataSourceService.connectTest(connectTestDTO);
    }

    /**
     * @author dwl
     * @param id 主键ID
     * @return R
     * @description 删除数据源
     */
    @ApiOperation("删除数据源")
    @DeleteMapping("/delete/deleteSource/{id}")
    public R<?> deleteSource(@PathVariable Long id){
        return dataSourceService.deleteSource(id);
    }
}
